﻿using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace FPSO.Models.ViewModel
{
    public class OPCMonitorViewModel : NotificationObject
    {
        public OPCMonitorViewModel()
        {
            opcValueList = new List<OPCTagViewModel>();
            opcValueList_tankStatus = new List<OPCTagViewModel>();
			opcValueList_flammableGasStatus = new List<OPCTagViewModel>();
			opcValueList_valveStatus = new List<OPCTagViewModel>();
            opcValueList_pumpStatus = new List<OPCTagViewModel>();
            opcValueList_hardwireStatus = new List<OPCTagViewModel>();

            opcControlList = new List<OPCTagViewModel>();

            modelDic = new Dictionary<string, OPCTagViewModel>();
        }

		private List<OPCTagViewModel> opcControlList;
        public List<OPCTagViewModel> OPCControlList
        {
            get { return opcControlList; }
            set
            {
                opcControlList = value;
                RaisePropertyChanged("OPCControlList");
            }
        }

        private List<OPCTagViewModel> opcValueList;
        public List<OPCTagViewModel> OPCValueList
        {
            get { return opcValueList; }
            set
            {
                opcValueList = value;
                RaisePropertyChanged("OPCValueList");
            }
        }
        private List<OPCTagViewModel> opcValueList_tankStatus;
        public List<OPCTagViewModel> OPCValueList_TankStatus
        {
            get { return opcValueList_tankStatus; }
            set
            {
                opcValueList_tankStatus = value;
                RaisePropertyChanged("OPCValueList_TankStatus");
            }
        }
        private List<OPCTagViewModel> opcValueList_valveStatus;
        public List<OPCTagViewModel> OPCValueList_ValveStatus
        {
            get { return opcValueList_valveStatus; }
            set
            {
                opcValueList_valveStatus = value;
                RaisePropertyChanged("OPCValueList_ValveStatus");
            }
        }
        private List<OPCTagViewModel> opcValueList_pumpStatus;
        public List<OPCTagViewModel> OPCValueList_PumpStatus
        {
            get { return opcValueList_pumpStatus; }
            set
            {
                opcValueList_pumpStatus = value;
                RaisePropertyChanged("OPCValueList_PumpStatus");
            }
        }
        private List<OPCTagViewModel> opcValueList_hardwireStatus;
        public List<OPCTagViewModel> OPCValueList_HardwireStatus
        {
            get { return opcValueList_hardwireStatus; }
            set
            {
                opcValueList_hardwireStatus = value;
                RaisePropertyChanged("OPCValueList_HardwireStatus");
            }
        }
		private List<OPCTagViewModel> opcValueList_flammableGasStatus;
		public List<OPCTagViewModel> OPCValueList_FlammableGasStatus
		{
			get { return opcValueList_flammableGasStatus; }
			set
			{
				opcValueList_flammableGasStatus = value;
				RaisePropertyChanged("OPCValueList_FlammableGasStatus");
			}
		}

		private Dictionary<string, OPCTagViewModel> modelDic;
        public void SetOpcTagValue(string tagName, DateTime tstamp, string valStr, string qualityStr)
        {
            if (modelDic.ContainsKey(tagName))
            {
                OPCTagViewModel tagView = modelDic[tagName];
                tagView.Tstamp = tstamp;
                tagView.ValStr = valStr;
                tagView.QualityStr = qualityStr;
            }
            else
            {
                OPCTagViewModel viewModel = new OPCTagViewModel();
                viewModel.TagName = tagName;
                viewModel.Tstamp = tstamp;
                viewModel.ValStr = valStr;
                viewModel.QualityStr = qualityStr;

                modelDic.Add(tagName, viewModel);
                OPCValueList.Add(viewModel);
            }
        }
        public void SetOpcTagValue_TankStatus(string tagName, DateTime tstamp, string valStr, string qualityStr,Color bgcolor)
        {
            if (modelDic.ContainsKey(tagName))
            {
                OPCTagViewModel tagView = modelDic[tagName];
                tagView.Tstamp = tstamp;
                tagView.ValStr = valStr;
                tagView.QualityStr = qualityStr;
                tagView.StatusColor = new SolidColorBrush(bgcolor);
            }
            else
            {
                OPCTagViewModel viewModel = new OPCTagViewModel();
                viewModel.TagName = tagName;
                viewModel.Tstamp = tstamp;
                viewModel.ValStr = valStr;
                viewModel.QualityStr = qualityStr;
                viewModel.StatusColor = new SolidColorBrush(bgcolor);

                modelDic.Add(tagName, viewModel);
                OPCValueList_TankStatus.Add(viewModel);
            }
        }
        public void SetOpcTagValue_ValveStatus(string tagName, DateTime tstamp, string valStr, string qualityStr, Color bgcolor)
        {
            if (modelDic.ContainsKey(tagName))
            {
                OPCTagViewModel tagView = modelDic[tagName];
                tagView.Tstamp = tstamp;
                tagView.ValStr = valStr;
                tagView.QualityStr = qualityStr;
                tagView.StatusColor = new SolidColorBrush(bgcolor);
            }
            else
            {
                OPCTagViewModel viewModel = new OPCTagViewModel();
                viewModel.TagName = tagName;
                viewModel.Tstamp = tstamp;
                viewModel.ValStr = valStr;
                viewModel.QualityStr = qualityStr;
                viewModel.StatusColor = new SolidColorBrush(bgcolor);

                modelDic.Add(tagName, viewModel);
                OPCValueList_ValveStatus.Add(viewModel);
            }
        }
        public void SetOpcTagValue_PumpStatus(string tagName, DateTime tstamp, string valStr, string qualityStr, Color bgcolor)
        {
            if (modelDic.ContainsKey(tagName))
            {
                OPCTagViewModel tagView = modelDic[tagName];
                tagView.Tstamp = tstamp;
                tagView.ValStr = valStr;
                tagView.QualityStr = qualityStr;
                tagView.StatusColor = new SolidColorBrush(bgcolor);
            }
            else
            {
                OPCTagViewModel viewModel = new OPCTagViewModel();
                viewModel.TagName = tagName;
                viewModel.Tstamp = tstamp;
                viewModel.ValStr = valStr;
                viewModel.QualityStr = qualityStr;
                viewModel.StatusColor = new SolidColorBrush(bgcolor);

                modelDic.Add(tagName, viewModel);
                OPCValueList_PumpStatus.Add(viewModel);
            }
        }
        public void SetOpcTagValue_HardwireStatus(string tagName, DateTime tstamp, string valStr, string qualityStr, Color bgcolor)
        {
            if (modelDic.ContainsKey(tagName))
            {
                OPCTagViewModel tagView = modelDic[tagName];
                tagView.Tstamp = tstamp;
                tagView.ValStr = valStr;
                tagView.QualityStr = qualityStr;
                tagView.StatusColor = new SolidColorBrush(bgcolor);
            }
            else
            {
                OPCTagViewModel viewModel = new OPCTagViewModel();
                viewModel.TagName = tagName;
                viewModel.Tstamp = tstamp;
                viewModel.ValStr = valStr;
                viewModel.QualityStr = qualityStr;
                viewModel.StatusColor = new SolidColorBrush(bgcolor);

                modelDic.Add(tagName, viewModel);
                OPCValueList_HardwireStatus.Add(viewModel);
            }
        }
		public void SetOpcTagValue_FlammableGasStatus(string tagName, DateTime tstamp, string valStr, string qualityStr, Color bgcolor)
		{
			if (modelDic.ContainsKey(tagName))
			{
				OPCTagViewModel tagView = modelDic[tagName];
				tagView.Tstamp = tstamp;
				tagView.ValStr = valStr;
				tagView.QualityStr = qualityStr;
				tagView.StatusColor = new SolidColorBrush(bgcolor);
			}
			else
			{
				OPCTagViewModel viewModel = new OPCTagViewModel();
				viewModel.TagName = tagName;
				viewModel.Tstamp = tstamp;
				viewModel.ValStr = valStr;
				viewModel.QualityStr = qualityStr;
				viewModel.StatusColor = new SolidColorBrush(bgcolor);

				modelDic.Add(tagName, viewModel);
				OPCValueList_FlammableGasStatus.Add(viewModel);
			}
		}

		public void SetOpcTagValue_CtrlPnts(string tagName, DateTime tstamp, string valStr, string qualityStr, Color bgcolor)
        {
            //bool contains = false;
            if (modelDic.ContainsKey(tagName))
            {
                //for(int i=0;i< OPCControlList.Count;i++)
                //{
                //    OPCTagViewModel tabView = OPCControlList[i];
                //    if (tabView.TagName == tagName)
                    {
                        OPCTagViewModel tagView0 = modelDic[tagName];
                        tagView0.Tstamp = tstamp;
                        tagView0.ValStr = valStr;
                        tagView0.QualityStr = qualityStr;
                        tagView0.StatusColor = new SolidColorBrush(bgcolor);
                        //contains = true;
                        //break;
                    }
                //}
                //if(contains == false)
                //{
                //    OPCTagViewModel viewModel = new OPCTagViewModel();
                //    viewModel.TagName = tagName;
                //    viewModel.Tstamp = tstamp;
                //    viewModel.ValStr = valStr;
                //    viewModel.QualityStr = qualityStr;
                //    viewModel.StatusColor = new SolidColorBrush(bgcolor);
                //    OPCControlList.Add(viewModel);
                //}
            }
            else
            {
                OPCTagViewModel viewModel = new OPCTagViewModel();
                viewModel.TagName = tagName;
                viewModel.Tstamp = tstamp;
                viewModel.ValStr = valStr;
                viewModel.QualityStr = qualityStr;
                viewModel.StatusColor = new SolidColorBrush(bgcolor);

                modelDic.Add(tagName, viewModel);
                OPCControlList.Add(viewModel);
            }
        }
    }

    public class OPCTagViewModel : NotificationObject
    {
        public OPCTagViewModel()
        { }

        private string tagName;
        private DateTime tstamp;
        private string valStr;
        private string qualityStr;
        private SolidColorBrush statusColor;

        public string TagName
        {
            get { return tagName; }
            set
            {
                tagName = value;
                RaisePropertyChanged("TagName");
            }
        }

        public DateTime Tstamp
        {
            get { return tstamp; }
            set
            {
                tstamp = value;
                RaisePropertyChanged("Tstamp");
            }
        }

        public string ValStr
        {
            get { return valStr; }
            set
            {
                valStr = value;
                RaisePropertyChanged("ValStr");
            }
        }

        public string QualityStr
        {
            get { return qualityStr; }
            set
            {
                qualityStr = value;
                RaisePropertyChanged("QualityStr");
            }
        }

        public SolidColorBrush StatusColor
        {
            get { return statusColor; }
            set
            {
                statusColor = value;
                RaisePropertyChanged("StatusColor");
            }
        }
    }
}
